package sparsing;

import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class Street {

	private int _id;
	private int _fromID;
	private int _toID;
	private double _km;
	private short _typ;
	private short _factor;
	private short _direction;

	public Street() {
		this(0, 0, 0, 0.0, (short) 0, (short) 0, (short) 0);
	}

	public Street(int id, int fromID, int toID, double km, short typ,
			short factor, short direction) {
		_id = id;
		_fromID = fromID;
		_toID = toID;
		_km = km;
		_typ = typ;
		_factor = factor;
		_direction = direction;
	}

	public int getID() {
		return _id;
	}

	public void setId(int id) {
		this._id = id;
	}

	public int getFromID() {
		return _fromID;
	}

	public void setFromID(int fromID) {
		this._fromID = fromID;
	}

	public int getToID() {
		return _toID;
	}

	public void setToID(int toID) {
		this._toID = toID;
	}

	public double getKm() {
		return _km;
	}

	public void setKm(double km) {
		this._km = km;
	}

	public short getTyp() {
		return _typ;
	}

	public void setTyp(short typ) {
		this._typ = typ;
	}

	public short getFactor() {
		return _factor;
	}

	public void setFactor(short factor) {
		this._factor = factor;
	}

	public short getDirection() {
		return _direction;
	}

	public void setDirection(short direction) {
		this._direction = direction;
	}

	public boolean bidir() {
		return _direction == 0;
	}

	public boolean outgoing(int node) {

		if (_fromID == node && _direction == 1) {
			return true;
		}

		if (_toID == node && _direction == 2) {
			return true;
		}

		return false;
	}

	public void insertStreet(Connection con) {

		String sql = new String();
		sql += "INSERT INTO street_record "
				+ "(Intnr,VonIntnr,BisIntnr,Km,RoadClass,FaktorPKW,Richtung)"
				+ " VALUES (" + _id + "," + _fromID + "," + _toID + "," + _km
				+ "," + _typ + "," + _factor + "," + _direction + ")";

		Statement stmt = null;

		try {

			stmt = con.createStatement();
			stmt.executeUpdate(sql);

		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if (stmt != null)
					stmt.close();
			} catch (Exception e) {
			}
		}

	}

	public static void deleteStreets(Connection con, Street s1, Street s2) {

		String sql = new String();
		sql += "DELETE FROM street_record WHERE Intnr=" + s1.getID()
				+ " OR Intnr=" + s2.getID();

		Statement stmt = null;

		try {

			stmt = con.createStatement();
			stmt.executeUpdate(sql);

		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if (stmt != null)
					stmt.close();
			} catch (Exception e) {
			}
		}

	}

	public static void getStreets(Connection con, int node, Street[] streets) {

		String sql = new String();
		sql += "SELECT Intnr,RoadClass,Km,FaktorPKW,Richtung,VonIntnr,BisIntnr"
				+ " FROM street_record WHERE VonIntnr=" + node
				+ " OR BisIntnr=" + node;

		Statement stmt = null;
		ResultSet rs = null;

		try {

			stmt = con.createStatement();
			rs = stmt.executeQuery(sql.toString());

			int i = 0;
			while (rs.next()) {

				int id = rs.getInt("Intnr");
				int fromID = rs.getInt("VonIntnr");
				int toID = rs.getInt("BisIntnr");
				double km = rs.getDouble("Km");
				short typ = rs.getShort("RoadClass");
				short factor = rs.getShort("FaktorPKW");
				short direction = rs.getShort("Richtung");

				streets[i++] = new Street(id, fromID, toID, km, typ, factor,
						direction);
			}

		} catch (SQLException e) {
			e.printStackTrace();
			try {
				PrintWriter pw = new PrintWriter("D:\\erroneous-sql.txt");
				pw.append(sql);
				pw.flush();
				pw.close();
			} catch (FileNotFoundException e1) {
				System.err.println("Could not write erroneous-sql.txt");
			}
			System.exit(-1);
		} catch (ArrayIndexOutOfBoundsException e) {
			System.out.println("Da passt was mim Grad net bei Knoten " + node);
		} finally {

			try {
				if (rs != null)
					rs.close();
			} catch (Exception e) {
			}
			try {
				if (stmt != null)
					stmt.close();
			} catch (Exception e) {
			}
		}
	}

}
